package base;

import java.util.Scanner;

public class BASIC12 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for (int i = 0; i < n; i++) {
			String str = sc.next();
			System.out.println(toOct(str));
		}
		sc.close();
	}

	public static long toOct(String val) {
		long  a = 0;
		int count = 0;
		for(int i = val.length() - 1; i >= 0; i--) {
			int num = toInt(val.charAt(i));
			a += num * (int)Math.pow(16, count++);
		}
		return a;
	}

	public static int toInt(char ch) {
		if (Character.isDigit(ch)) {
			return ch - '0';
		}
		return ch - 'A' + 10;
	}
}
